package com.zzb.framework.role.controller;

import com.zzb.framework.frameworkcontroller.common.FrameworkBaseController;
import com.zzb.framework.role.entity.IUser;
import com.zzb.framework.role.entity.SysRoleUser;

import com.zzb.framework.role.form.SysRoleUserSearchForm;
import com.zzb.framework.role.service.SysRoleUserService;
import com.zzb.framework.common.BasePage;
import com.zzb.framework.common.BaseResponse;
import com.zzb.framework.common.ServiceException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
* 用户角色关系表controller
*/
@Controller
@RequestMapping("/framework/sysRoleUser")
public class SysRoleUserController extends FrameworkBaseController {
    private static final Logger logger = LoggerFactory.getLogger(SysRoleUserController.class);
    @Autowired
    private SysRoleUserService sysRoleUserService;

    /**用户角色关系表管理首页*/
    @RequestMapping({"","/","/index"})
    public String index(){
        return "framework/sysRoleUser/index";
    }
    /**用户角色关系表列表json*/
    @RequestMapping("list")
    @ResponseBody
    public BasePage<SysRoleUser> list(SysRoleUserSearchForm sysRoleUserSearchForm){
        return sysRoleUserService.queryPage(sysRoleUserSearchForm);
    }

    /**用户角色关系表表单页面*/
    @RequestMapping("form")
    public String form(String id, Model model){
        SysRoleUser sysRoleUser;
        if(StringUtils.isEmpty(id)){
            sysRoleUser = new SysRoleUser();
    } else{
        sysRoleUser = sysRoleUserService.get(id);
        if(sysRoleUser == null) sysRoleUser = new SysRoleUser();
    }
        model.addAttribute("sysRoleUser", sysRoleUser);
        return "framework/sysRoleUser/form";
    }

    /**用户角色关系表保存*/
    @RequestMapping("/save")
    @ResponseBody
    public BaseResponse<SysRoleUser> save(SysRoleUser sysRoleUser, HttpServletRequest request){
        try {
            IUser user = getCurrentUser(request);
            return sysRoleUserService.saveRoleUser(sysRoleUser, user);
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse<>(BaseResponse.FAIL,e.getMessage());
        }
    }

    /**用户角色关系表删除*/
    @RequestMapping("/delete")
    @ResponseBody
    public BaseResponse delete(String id,HttpServletRequest request){
        try {
            return sysRoleUserService.deleteRoleUser(id,getCurrentUser(request));
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL,e.getMessage());
        }
    }

    /**用户角色关系表开启*/
    @RequestMapping("/open")
    @ResponseBody
    public BaseResponse open(String id ,HttpServletRequest request){
        try {
            return sysRoleUserService.recoveryById(id,getCurrentUser(request));
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL,e.getMessage());
        }
    }

    /**用户角色关系表屏蔽*/
    @RequestMapping("/deny")
    @ResponseBody
    public BaseResponse deny(String id ,HttpServletRequest request){
        try {
            return sysRoleUserService.trashById(id,getCurrentUser(request));
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL,e.getMessage());
        }
    }

}
